//
//  PKPlayListTableViewController.m
//  AMomentApp
//
//  Created by lanou on 15/10/6.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKPlayListTableViewController.h"
#import "PKPlayListTableViewCell.h"
@interface PKPlayListTableViewController ()

@end

@implementation PKPlayListTableViewController

-(NSArray *)dataSourceArray
{
    if (_dataArray == nil) {
        self.dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.tableView registerClass:[PKPlayListTableViewCell class] forCellReuseIdentifier:@"PKPlayListTableViewCell"];
    self.tableView.rowHeight = 60;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

// 更新数据
- (void)UPDateData:(NSArray *)array
{
    self.dataArray = array;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKPlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKPlayListTableViewCell" forIndexPath:indexPath];
    
    PKRadioDetailModel *model = _dataArray[indexPath.row];
    cell.configureModel = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i =0; i < self.dataArray.count; i++) {
        PKRadioDetailModel *model = self.dataArray[i];
        model.isSelected = NO;
        PKPlayListTableViewCell *cell = (PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        cell.leftView.backgroundColor = [UIColor whiteColor];
    }
    PKPlayListTableViewCell *cell = (PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.leftView.backgroundColor = [UIColor greenColor];
    
    PKRadioDetailModel *model = self.dataArray[indexPath.row];
    
    NSNumber *index = [NSNumber numberWithUnsignedInteger:indexPath.row];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeIndexPath" object:@"changeIndex" userInfo:[NSDictionary dictionaryWithObject:index forKey:@"index"]];
    model.isSelected = YES;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKPlayListTableViewCell *cell = (PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.leftView.backgroundColor = [UIColor whiteColor];
    
    PKRadioDetailModel *model = self.dataArray[indexPath.row];
    model.isSelected = NO;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
